package leetcode

import java.util.*

//https://leetcode.com/problems/valid-parentheses/

fun main(args: Array<String>) {
//    println('('.toInt())
//    println(')'.toInt())
//    println('{'.toInt())
//    println('}'.toInt())
//    println('['.toInt())
//    println(']'.toInt())
    println(isValid("("))
}

fun isValid(s: String): Boolean {

    val stack = arrayListOf<Char>()
    for (i in 0 until s.length) {
        val char = s[i]
        if (char == '{' || char == '(' || char == '[')
            stack.add(char)
        else {
            if (stack.isEmpty()) return false
            val topChar = stack.removeAt(stack.lastIndex)
            if (
                    (topChar == '{' && char != '}')
                    || (topChar == '[' && char != ']')
                    || (topChar == '(' && char != ')')
            ) return false
        }
    }
    return stack.isEmpty()
}